{% extends "layout.html" %}
{% block content %}
    <div class="x-nav">
        <div class="layui-col-md3">
            <button class="layui-btn" onclick="x_admin_show('添加数据点','dataItem2channel-add.html?channel_id='+channel_id,800,300)"><i class="layui-icon"></i>添加数据点</button>
        </div>
        <div class="layui-col-md3">
            <button class="layui-btn" id="upload_dataItem_table"><i class="layui-icon"></i>导入点表</button>
            <button class="layui-btn" id="download_template" onclick="javascript:window.open('../dataItem_templet.xls')">下载模板</button>
        </div>
          <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:30px">ဂ</i></a>
    </div>
    <div class="x-body layui-anim layui-anim-up">
      <table id="dataItem2channelList" class="layui-table" lay-filter="dataItem">
      </table>
      <script type="text/html" id="dataItemBar">
        <div class="layui-btn-group">
          <button type="button" class="layui-btn layui-btn-sm"lay-event="del"><i class="layui-icon">&#xe640;</i></button>
        </div>
      </script>    
    </div>
    <script>
        var dataItemTitle;
        var channel_id=GetQueryString('channel_id');
        layui.use(['table','form','upload'], function(){
            var table = layui.table;
            var upload = layui.upload;
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.full(index); //执行最大化
            table.render({
                elem: '#dataItem2channelList'
                ,height:'full-120'
                ,url:'./dataItem2channelList?channel_id='+channel_id
                ,cols:[[
                {title:'ID',field:'dataItem2channel_id',align: 'center', sort: true}
                ,{title:'标签',field:'d2c_label',align: 'center', sort: true}
                ,{title:'原标签',field:'label',align: 'center'}
                ,{title:'数值',field:'value',align: 'center', sort: true}
                ,{title:'时间戳',field:'timestamp',align: 'center', sort: true}
                ,{fixed: 'right', align:'center', toolbar: '#dataItemBar'}
                ]]
                ,page: true
                ,limit:100
                ,limits:[100,200,500]
            });
            //监听工具条
            table.on('tool(dataItem)', function(obj){
                var data = obj.data;
                switch(obj.event){
                case 'del':
                    layer.confirm('真的删除行么', function(index){
                        data={'dataItem2channel_id':obj.data.dataItem2channel_id};
                        $.ajax({
                            url:'del_dataItem2channel',
                            type:'POST',
                            data:data,
                            success:function(data){
                                if(data.code==0)obj.del();
                            }
                        });
                        obj.del();
                        layer.close(index);
                    });
                    break;
                default:
                }
            });
            //执行实例
            var uploadInst = upload.render({
                elem: '#upload_dataItem_table' //绑定元素
                ,accept:'file'
                ,exts:'xls|xlsx'
                ,acceptMime:'application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
                ,url: 'upload_dataItem_table' //上传接口
                ,data:{
                    channel_id:channel_id
                }
                ,field:'upload_file'
                ,done: function(res){
                    //上传完毕回调
                    if(res.code==0){
                    layer.alert("导入成功-"+res.msg, {icon: 6},function () {
                            location.reload();
                        });
                    }else{
                        layer.alert("导入失败！"+res.msg, {icon: 6});
                    }
                }
                ,error: function(){
                    //请求异常回调
                    layer.alert("导入失败！请检查表格内容", {icon: 6});
                }
            });
        });
    </script>
{% endblock %}